package ynu.edu.online_course.mapper;

import org.apache.ibatis.annotations.*;
import ynu.edu.online_course.entity.Course;
import java.util.List;

@Mapper
public interface CourseMapper {

    // 根据ID查询课程
    @Select("SELECT * FROM course WHERE id = #{id}")
    @Results({
            @Result(column = "administrator", property = "administrator.userId"),
            @Result(column = "type_id", property = "courseType.id")
    })
    Course findById(@Param("id") Long id);

    // 插入课程
    @Insert("INSERT INTO course (course_name, discription, administrator, type_id) " +
            "VALUES (#{courseName}, #{discription}, #{administrator.userId}, #{courseType.id})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertCourse(Course course);

    // 更新课程信息
    @Update("UPDATE course SET course_name = #{courseName}, discription = #{discription}, " +
            "administrator = #{administrator.userId}, type_id = #{courseType.id} WHERE id = #{id}")
    int updateCourse(Course course);

    // 删除课程
    @Delete("DELETE FROM course WHERE id = #{id}")
    int deleteCourse(@Param("id") Long id);

    // 查询所有课程
    @Select("SELECT * FROM course")
    @Results({
            @Result(column = "administrator", property = "administrator.userId"),
            @Result(column = "type_id", property = "courseType.id"),
            @Result(column = "course_name", property = "courseName"),
            @Result(column = "description", property = "description")
    })
    List<Course> findAll();
}
